package com.kc.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.bus.domain.Goods;
import com.kc.bus.domain.Outport;
import com.kc.bus.domain.Provider;
import com.kc.bus.service.GoodsService;
import com.kc.bus.service.InportService;
import com.kc.bus.service.OutportService;
import com.kc.bus.service.ProviderService;
import com.kc.bus.vo.OutportVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-04
 */
@RestController
@RequestMapping("/outport")
public class OutportController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private InportService inportService;
    @Autowired
    private OutportService outportService;

        //添加退货信息
    @RequestMapping("addOutport")
    public R addOutport(Integer id,Integer number,String remark){
        try {
            outportService.addOutport(id,number,remark);
            return R.ok().message("退货成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("退货失败");
        }
    }

    @RequestMapping("loadAllOutport")
    public DataGridView loadAllOutport(OutportVo outportVo){

        IPage<Outport> page = new Page<>(outportVo.getPage(),outportVo.getLimit());
        QueryWrapper<Outport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(outportVo.getProviderid()!=null&&outportVo.getProviderid()!=0,"providerid",outportVo.getProviderid());
        queryWrapper.eq(outportVo.getGoodsid()!=null&&outportVo.getGoodsid()!=0,"goodsid",outportVo.getGoodsid());
        //大于ge
        queryWrapper.ge(outportVo.getStartTime()!=null, "outputtime", outportVo.getStartTime());
        //小于
        queryWrapper.le(outportVo.getEndTime()!=null, "outputtime", outportVo.getEndTime());
        queryWrapper.orderByDesc("outputtime");
        outportService.page(page,queryWrapper);
        List<Outport> records = page.getRecords();
        for (Outport outport : records) {
            Provider provider = providerService.getById(outport.getProviderid());
            if(null!=provider){
                outport.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(outport.getGoodsid());
            if (null!=goods){
                outport.setGoodsname(goods.getGoodsname());
                outport.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}

